package com.ktg.web;


import com.ktg.ktgcommon.domain.TmTool;
import com.ktg.ktgcommon.utils.Page;
import com.ktg.service.TmToolService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class TmToolController {
    @Resource
    public TmToolService service;
    @RequestMapping("/page")
    public Page<TmTool> page(TmTool entity,
                             @RequestParam(value = "pageNo" , defaultValue = "1")Integer pageNo,
                             @RequestParam(value = "pageSize" , defaultValue = "5")Integer pageSize){
        Page<TmTool> page = service.findPage(entity, pageNo, pageSize);
        return page;
    }

    @RequestMapping("/toolTypeNameList")
    public List<TmTool> toolTypeNameList(){
        List<TmTool> list = service.toolTypeNameList();
        return list;
    }

    @PostMapping("/doSave")
    public Map save(TmTool entity){
        Map map = new HashMap();
        boolean ok = false;
        if(entity.getToolTypeId() == null){
            ok = service.add(entity);
        }else{
            ok = service.update(entity);
        }
        if(ok){
            map.put("code",0);
        }else{
            map.put("code",1);
        }
        return map;
    }

    @PostMapping("/echo")
    public Map detail(@RequestBody Map params) {
        Map map = new HashMap();
        Object toolId = params.get("toolId");

        if(toolId != null){
            long ttid = Long.parseLong(toolId.toString());
            TmTool entity = service.findById(ttid);
            map.put("code", 200);
            map.put("data", entity);
        }else{
            map.put("code", 500);
        }
        return map;
    }
}
